﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Taxes.DAL
{
    /// <summary>
    /// 数据访问类:revenue_info
    /// </summary>
    public partial class revenue_info
    {
        public revenue_info()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "revenue_info");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from revenue_info");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool GetRegionID(decimal lon, decimal lat,int ID)
        {
            int regionID = 0;
            SqlParameter [] parm = new SqlParameter[]
            {
                new SqlParameter("@pnt_x",SqlDbType.Decimal,18)
                {
                    Value=lon
                },
                new SqlParameter("@pnt_y",SqlDbType.Decimal,18)
                {
                    Value= lat
                }
                ,
                new SqlParameter("@revenue_id",SqlDbType.Int,4)
                {
                    Value=ID
                }

               

            };
            int effect ;
            DbHelperSQL.RunProcedure("up_getRegionIdByPnt",parm, out effect);
            return effect>0;
        }

        public void GetRevenueInfo(decimal lon, decimal lat, int industryID,ref String regionName, ref decimal region_unit_price, ref decimal region_unit_rent, ref int region_houseold, ref decimal industry_unit_rent, ref int industry_household, ref decimal tax_loaded_region, ref decimal tax_loaded_industry,ref int regionID)
        {
            SqlParameter [] parm  = new SqlParameter[]
            {
                new SqlParameter(){ Direction=ParameterDirection.Input, Value=lon,ParameterName="@pnt_x" , DbType=DbType.Decimal,Size=18},
                new SqlParameter(){Direction= ParameterDirection.Input,Value=lat,ParameterName="@pnt_y",DbType= DbType.Decimal,Size=18},
                new SqlParameter(){Direction= ParameterDirection.Input,Value=industryID,ParameterName="@industry_id",DbType= DbType.Int32,Size=4},
                new SqlParameter(){ Direction= ParameterDirection.Output,ParameterName= "@region_name",Value=regionName,DbType= DbType.String, Size= 50  },
                new SqlParameter(){Direction= ParameterDirection.Output,Value=region_unit_price, DbType= DbType.Decimal, ParameterName="@region_unit_price", Size=18},
                new SqlParameter(){Direction= ParameterDirection.Output ,Value= region_unit_rent,DbType= DbType.Decimal,ParameterName="@region_unit_rent",Size=18},
                new SqlParameter(){ Direction= ParameterDirection.Output,Value= region_houseold,DbType= DbType.Int32,Size=4,ParameterName="@region_household"},
                new SqlParameter(){ Direction= ParameterDirection.Output,Value=industry_unit_rent,DbType= DbType.Decimal,Size=18,ParameterName="@industry_unit_rent"},
                new SqlParameter(){Direction= ParameterDirection.Output,Value=industry_household,DbType= DbType.Int32,Size=4, ParameterName="@industry_household"},
                new SqlParameter(){Direction= ParameterDirection.Output,Value=tax_loaded_region,DbType= DbType.Decimal,Size=18, ParameterName="@tax_loaded_region"},
                new SqlParameter(){Direction= ParameterDirection.Output,Value=tax_loaded_industry,DbType= DbType.Decimal,Size=18,ParameterName="@tax_loaded_industry"},
                new SqlParameter(){ Direction= ParameterDirection.Output,DbType= DbType.Int32,Size=4,ParameterName="@region_id"}
            };
            DbHelperSQL.RunProcedure("up_getRevenueInfo",parm);
            regionName = parm[3].Value.ToString();
            region_unit_price =parm[4].ToString()==string.Empty? 0:Convert.ToDecimal( parm[4].Value.ToString());
            region_unit_rent = parm[5].ToString()==string.Empty? 0:Convert.ToDecimal(parm[5].Value.ToString());
            region_houseold = parm[6].ToString() == string.Empty ? 0 : Convert.ToInt32(parm[6].Value.ToString());
            industry_unit_rent = parm[7].ToString() == string.Empty ? 0 : Convert.ToDecimal(parm[7].Value.ToString());
            industry_household = parm[8].ToString() == string.Empty ? 0 : Convert.ToInt32(parm[8].Value.ToString());
            tax_loaded_region = parm[9].ToString() == string.Empty ? 0 : Convert.ToDecimal(parm[9].Value.ToString());
            tax_loaded_industry =parm[10].ToString()==string.Empty? 0: Convert.ToDecimal( parm[10].Value.ToString());
            regionID = Convert.ToInt32(parm[11].Value.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Taxes.Model.revenue_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into revenue_info(");
            strSql.Append("reve_code,reve_name,reve_address,reve_lon,reve_lat,industry_id,region_id,reve_pic,reve_trent,reve_area,reve_area_pic,reve_operator,in_date,pc_code,register_date,register_status,register_type)");
            strSql.Append(" values (");
            strSql.Append("@reve_code,@reve_name,@reve_address,@reve_lon,@reve_lat,@industry_id,@region_id,@reve_pic,@reve_trent,@reve_area,@reve_area_pic,@reve_operator,@in_date,@pc_code,@register_date,@register_status,@register_type)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@reve_code", SqlDbType.VarChar,50),
					new SqlParameter("@reve_name", SqlDbType.VarChar,50),
					new SqlParameter("@reve_address", SqlDbType.VarChar,200),
					new SqlParameter("@reve_lon", SqlDbType.Decimal,9),
					new SqlParameter("@reve_lat", SqlDbType.Decimal,9),
					new SqlParameter("@industry_id", SqlDbType.Int,4),
					new SqlParameter("@region_id", SqlDbType.Int,4),
					new SqlParameter("@reve_pic", SqlDbType.VarChar,200),
					new SqlParameter("@reve_trent", SqlDbType.Decimal,9),
					new SqlParameter("@reve_area", SqlDbType.Decimal,9),
					new SqlParameter("@reve_area_pic", SqlDbType.VarChar,200),
					new SqlParameter("@reve_operator", SqlDbType.Int,4),
					new SqlParameter("@in_date", SqlDbType.DateTime),
                                       new SqlParameter("@pc_code", SqlDbType.VarChar),
                                        new SqlParameter("@register_date", SqlDbType.DateTime),
                                        new SqlParameter("@register_status", SqlDbType.VarChar),
                                         new SqlParameter("@register_type", SqlDbType.VarChar)};
            parameters[0].Value = model.reve_code;
            parameters[1].Value = model.reve_name;
            parameters[2].Value = model.reve_address;
            parameters[3].Value = model.reve_lon;
            parameters[4].Value = model.reve_lat;
            parameters[5].Value = model.industry_id;
            parameters[6].Value = model.region_id;
            parameters[7].Value = model.reve_pic;
            parameters[8].Value = model.reve_trent;
            parameters[9].Value = model.reve_area;
            parameters[10].Value = model.reve_area_pic;
            parameters[11].Value = model.reve_operator;
            parameters[12].Value = model.in_date;
            parameters[13].Value = model.PC_Code;
            parameters[14].Value = model.Register_Date;
            parameters[15].Value = model.Register_status;
            parameters[16].Value = model.Register_type;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Taxes.Model.revenue_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update revenue_info set ");
            strSql.Append("reve_code=@reve_code,");
            strSql.Append("reve_name=@reve_name,");
            strSql.Append("reve_address=@reve_address,");
            strSql.Append("reve_lon=@reve_lon,");
            strSql.Append("reve_lat=@reve_lat,");
            strSql.Append("industry_id=@industry_id,");
            strSql.Append("region_id=@region_id,");
            strSql.Append("reve_pic=@reve_pic,");
            strSql.Append("reve_trent=@reve_trent,");
            strSql.Append("reve_area=@reve_area,");
            strSql.Append("reve_area_pic=@reve_area_pic,");
            strSql.Append("reve_operator=@reve_operator,");
            strSql.Append("in_date=@in_date");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@reve_code", SqlDbType.VarChar,50),
					new SqlParameter("@reve_name", SqlDbType.VarChar,50),
					new SqlParameter("@reve_address", SqlDbType.VarChar,200),
					new SqlParameter("@reve_lon", SqlDbType.Decimal,9),
					new SqlParameter("@reve_lat", SqlDbType.Decimal,9),
					new SqlParameter("@industry_id", SqlDbType.Int,4),
					new SqlParameter("@region_id", SqlDbType.Int,4),
					new SqlParameter("@reve_pic", SqlDbType.VarChar,200),
					new SqlParameter("@reve_trent", SqlDbType.Decimal,9),
					new SqlParameter("@reve_area", SqlDbType.Decimal,9),
					new SqlParameter("@reve_area_pic", SqlDbType.VarChar,200),
					new SqlParameter("@reve_operator", SqlDbType.Int,4),
					new SqlParameter("@in_date", SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.reve_code;
            parameters[1].Value = model.reve_name;
            parameters[2].Value = model.reve_address;
            parameters[3].Value = model.reve_lon;
            parameters[4].Value = model.reve_lat;
            parameters[5].Value = model.industry_id;
            parameters[6].Value = model.region_id;
            parameters[7].Value = model.reve_pic;
            parameters[8].Value = model.reve_trent;
            parameters[9].Value = model.reve_area;
            parameters[10].Value = model.reve_area_pic;
            parameters[11].Value = model.reve_operator;
            parameters[12].Value = model.in_date;
            parameters[13].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from revenue_info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from revenue_info ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Taxes.Model.revenue_info GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,reve_code,reve_name,reve_address,reve_lon,reve_lat,industry_id,region_id,reve_pic,reve_trent,reve_area,reve_area_pic,reve_operator,in_date from revenue_info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            Taxes.Model.revenue_info model = new Taxes.Model.revenue_info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reve_code"] != null && ds.Tables[0].Rows[0]["reve_code"].ToString() != "")
                {
                    model.reve_code = ds.Tables[0].Rows[0]["reve_code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["reve_name"] != null && ds.Tables[0].Rows[0]["reve_name"].ToString() != "")
                {
                    model.reve_name = ds.Tables[0].Rows[0]["reve_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["reve_address"] != null && ds.Tables[0].Rows[0]["reve_address"].ToString() != "")
                {
                    model.reve_address = ds.Tables[0].Rows[0]["reve_address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["reve_lon"] != null && ds.Tables[0].Rows[0]["reve_lon"].ToString() != "")
                {
                    model.reve_lon = decimal.Parse(ds.Tables[0].Rows[0]["reve_lon"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reve_lat"] != null && ds.Tables[0].Rows[0]["reve_lat"].ToString() != "")
                {
                    model.reve_lat = decimal.Parse(ds.Tables[0].Rows[0]["reve_lat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["industry_id"] != null && ds.Tables[0].Rows[0]["industry_id"].ToString() != "")
                {
                    model.industry_id = int.Parse(ds.Tables[0].Rows[0]["industry_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["region_id"] != null && ds.Tables[0].Rows[0]["region_id"].ToString() != "")
                {
                    model.region_id = int.Parse(ds.Tables[0].Rows[0]["region_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reve_pic"] != null && ds.Tables[0].Rows[0]["reve_pic"].ToString() != "")
                {
                    model.reve_pic = ds.Tables[0].Rows[0]["reve_pic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["reve_trent"] != null && ds.Tables[0].Rows[0]["reve_trent"].ToString() != "")
                {
                    model.reve_trent = decimal.Parse(ds.Tables[0].Rows[0]["reve_trent"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reve_area"] != null && ds.Tables[0].Rows[0]["reve_area"].ToString() != "")
                {
                    model.reve_area = decimal.Parse(ds.Tables[0].Rows[0]["reve_area"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reve_area_pic"] != null && ds.Tables[0].Rows[0]["reve_area_pic"].ToString() != "")
                {
                    model.reve_area_pic = ds.Tables[0].Rows[0]["reve_area_pic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["reve_operator"] != null && ds.Tables[0].Rows[0]["reve_operator"].ToString() != "")
                {
                    model.reve_operator = int.Parse(ds.Tables[0].Rows[0]["reve_operator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["in_date"] != null && ds.Tables[0].Rows[0]["in_date"].ToString() != "")
                {
                    model.in_date = DateTime.Parse(ds.Tables[0].Rows[0]["in_date"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,reve_code,reve_name,reve_address,reve_lon,reve_lat,industry_id,region_id,reve_pic,reve_trent,reve_area,reve_area_pic,reve_operator,in_date ");
            strSql.Append(" FROM revenue_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,reve_code,reve_name,reve_address,reve_lon,reve_lat,industry_id,region_id,reve_pic,reve_trent,reve_area,reve_area_pic,reve_operator,in_date ");
            strSql.Append(" FROM revenue_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "revenue_info";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

